package LevelEditor.main;

import basis.game.Camera.Camera;
import basis.game.main.Core;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import unamed.level.Room;

public class Editor extends Core{
    
    public static void main(String[] args){
        Editor e=new Editor();
        e.init();
        e.loop();
    }
    
    private Selection sel;
    private Plane p;
    private Camera c;
    private final static int SEL=0,BRUSH=1,ERASE=2,MOVE=3;
    private int mode=SEL;
    private double a,b,z=1,mx,my;
    
    @Override
    public void init(){
        width=700;
        height=622;
        title="Level Editor";
        FRAMELIMIT=60;
        super.init();
        window.addMouseListener(this);
        window.addMouseMotionListener(this);
        window.addMouseWheelListener(this);
        window.addKeyListener(this);
        c=new Camera(0,22,width,height-22);
        p=new Plane(0,22,width,height-22);
        sel=new Selection(p);
    }
    
    @Override
    public void update(long t){
        c.update(p);
        sel.update(t);
        p.update(t);
    }
    
    @Override
    public void render(Graphics2D g){
        super.render(g);
        p.render(g);
        g.setColor(Color.LIGHT_GRAY);
        sel.render(g);
        g.setColor(Color.black);
        g.drawString("Fps:"+fps,20,40);
        g.drawString("Zoom:"+z,20,60);
    }
    
    @Override
    public void mousePressed(MouseEvent e){
        switch (mode){
            case SEL:
                sel.setPoint1(e.getX(),e.getY());
                sel.setPoint2(e.getX(),e.getY());
                break;
            case MOVE:
                a=e.getX()-c.getCenterX();
                b=e.getY()-c.getCenterY();
        }
    }
    
    @Override
    public void mouseMoved(MouseEvent e){
        mx=e.getX();
        my=e.getY();
    }
    
    @Override
    public void mouseDragged(MouseEvent e){
        switch (mode){
            case SEL:
                sel.setPoint2(e.getX(),e.getY());
                break;
            case MOVE:
                c.setCenterX(e.getX()-a);
                c.setCenterY(e.getY()-b);
        }
       
    }
    
    @Override
    public void mouseReleased(MouseEvent e){
        switch(mode){
            case SEL:
                sel.setPoint2(e.getX(),e.getY());
        }
    }
    
    @Override
    public void keyReleased(KeyEvent e){
        switch(e.getKeyCode()){
            case KeyEvent.VK_L:
                p.add(new Room(sel.getX(),sel.getY(),sel.getWidth(),sel.getHeight()));
                break;
            case KeyEvent.VK_1:
                mode=SEL;
                break;
            case KeyEvent.VK_2:
                mode=MOVE;
                break;
            case KeyEvent.VK_SPACE:
                c.setX(0);
                c.setY(22);
                break;
        }
    }
    
    @Override
    public void mouseWheelMoved(MouseWheelEvent e){
        if(e.getWheelRotation()<0){
            z+=.01;
        }else if(e.getWheelRotation()>0){
            z-=.01;
        }
    }
    
}